import {ref, createVNode} from "vue";
import axios from "@/utils/axios";
import {message, Modal} from "ant-design-vue";
import EveryPageEnum from "@/enums/everyPageEnum"
import {ExclamationCircleOutlined} from "@ant-design/icons-vue";

class DeviceQueryInfo{
    // sectionId: number | undefined = undefined

}

class DeviceFormState{
    sectionId: any | undefined = undefined
    kind: Array<number> = new Array<number>()
    status: number | undefined
    deviceName: string | undefined = undefined
    deviceSn: string | undefined = undefined
    deviceSearch: string | undefined = undefined
    workToday: number | undefined
    checkStatus: number | undefined = undefined
    startPage: number | undefined = 1
    everyPage: number | undefined = EveryPageEnum.small
}

class DeviceBaseInfo{
    deviceId:number = -1
    deviceName:string|undefined = ""
    deviceSn:  string | undefined = undefined
    status:number = 0
    deviceKind:number|undefined
    deviceColor:string = "#000000"
    sectionId:number|undefined
    deviceNumber:string|undefined
}

export default ()=>{
    const inDeviceBaseInfo:any = ref()
    // 表单对象
    const deviceRef = ref()
    // 表单内容
    const deviceFormState = ref(new DeviceFormState())
    // 当前已查询条件
    const deviceFormState2 = ref(new DeviceFormState())
    // 设备列表
    const deviceList:any = ref({})
    // 审核中、已通过的车辆不可修改信息
    const updateDevInfo:any = ref(false)

    /**
     * 重置表单
     */
    const resetForm = ()=>{
        deviceFormState.value = new DeviceFormState()
    }

    /**
     * 获取设备列表
     */
    const getDeviceList = ()=>{
        if(typeof deviceFormState.value.deviceName != 'undefined')
            deviceFormState.value.deviceName =
                (deviceFormState.value.deviceName as string).replace('/\\s+/g','')===""?undefined:deviceFormState.value.deviceName
        if(typeof deviceFormState.value.deviceSn != 'undefined')
            deviceFormState.value.deviceSn =
                (deviceFormState.value.deviceSn as string).replace('/\\s+/g','')===""?undefined:deviceFormState.value.deviceSn
        return new Promise((resolve,reject)=>{
            axios.post("/beta/findDevice",
                deviceFormState.value).then((res:any)=>{
                if(res.success){
                    deviceFormState2.value = deviceFormState.value
                    deviceList.value = res.data
                    resolve(res)
                }else{
                    message.info(res.msg)
                    reject(false)
                }
            })
        })
    }
    /**
     * 跳转下一页
     * todo 这边的查询都写乱了,有时间重新整理一下
     */
    const nextDeviceList = (pageInfo:any)=>{
        deviceFormState2.value.startPage = pageInfo.current
        axios.post("/beta/findDevice",
            deviceFormState2.value).then((res:any)=>{
            if(res.success){
                deviceList.value = res.data
            }else{
                message.info(res.msg)
            }
        })
    }

    /**
     * 查询指定车辆基本信息
     * @param deviceId
     */
    const getDeviceBaseInfo = (deviceId:any)=>{
        return new Promise((resolve,reject)=>{
            axios.post("/beta/findDeviceLocationAndBuild",
                {deviceId:deviceId}).then((res:any)=>{
                if(res.success){
                    Object.assign(res.data,deviceId)
                    inDeviceBaseInfo.value = res.data
                    console.log(JSON.stringify(inDeviceBaseInfo.value))
                    resolve(res)
                }else{
                    message.info(res.msg)
                    reject(false)
                }
            })
        })
    }

    // 表单对象
    const deviceBaseFormRef = ref()
    // 表单模态框
    const showDeviceBaseModal = ref(false)
    // 表单信息
    const deviceBaseForm = ref(new DeviceBaseInfo())
    // 表单验证规则
    const driverBaseRules = {
        deviceName:[{
            required: true,
            message: "设备名称不能为空",
            // message: '设备名称请在1-15字之间',
            type: 'string',
            trigger: 'blur'
        }],
        deviceKind:[{
            required: true,
            message: '请选择车辆类型',
            type: 'any',
            trigger: 'change'
        }],
        sectionId:[{
            required: true,
            message: '请选择归属标段',
            type: 'any',
            trigger: 'change'
        }],
        deviceNumber:[{
            required: true,
            message: '请输入车牌号',
            type: 'string',
            trigger: 'change'
        }]
    }
    /**
     * 新增||更新设备
     */
    const setDeviceBaseInfo = ()=>{
        deviceBaseFormRef.value.validate().then(()=>{
            axios.post("/beta/updateDeviceInfoByDeviceId",
                deviceBaseForm.value).then((res:any)=>{
                if(res.success){
                    if (deviceBaseForm.value.deviceId>0){
                        let inDevice = deviceList.value.deviceInfo[deviceList.value.deviceInfo.findIndex((item:any)=>{
                            return item.deviceId === deviceBaseForm.value.deviceId
                        })]
                        inDevice.deviceName = deviceBaseForm.value.deviceName
                        inDevice.deviceColor = deviceBaseForm.value.deviceColor
                    }
                    message.info("保存成功")
                    showDeviceBaseModal.value = false
                }else{
                    message.info(res.msg)
                }
            })
        }).catch((e:any)=>{
            console.log(e)
        })
    }
    /**
     * 设备退场
     * @param deviceId 设备id
     * @param callback 回调函数
     */
    const walkOff = (deviceId:number,callback:Function|null=null)=>{
        axios.post("/beta/updateDeviceInfoByDeviceId",
            {deviceId:deviceId, status:2}).then((res:any)=>{
            if(res.success){
                message.info("退场成功")
                showDeviceBaseModal.value = false
                if(callback) callback()
            }else{
                message.info(res.msg)
            }
        })
    }
    /**
     * 设备进场
     * @param deviceId 设备id
     * @param callback 回调函数
     */
    const walkon = (deviceId:number,callback:Function|null=null)=>{
        axios.post("/beta/updateDeviceInfoByDeviceId",
            {deviceId:deviceId, status:0}).then((res:any)=>{
            if(res.success){
                message.info("进场成功")
                showDeviceBaseModal.value = false
                if(callback) callback()
            }else{
                message.info(res.msg)
            }
        })
    }

    /**
     * 设备退场 模态框
     * @param deviceInfo 设备信息
     * @param callback 回调函数
     */
    const openWalkOff = (deviceInfo:any)=>{
        Modal.confirm({
            title: `是否退场名称为${deviceInfo.deviceName},SN为${deviceInfo.deviceSn}的设备?`,
            icon: createVNode(ExclamationCircleOutlined),
            okText: '确定',
            okType: 'danger',
            cancelText: '取消',
            onOk() {
                walkOff(deviceInfo.deviceId,()=>{
                    deviceInfo.deviceStatus = 2
                });
            }
        });
    }
    /**
     * 设备进场 模态框
     * @param deviceInfo 设备信息
     * @param callback 回调函数
     */
    const openWalkOn = (deviceInfo:any)=>{
        Modal.confirm({
            title: `是否进场名称为${deviceInfo.deviceName},SN为${deviceInfo.deviceSn}的设备?`,
            icon: createVNode(ExclamationCircleOutlined),
            okText: '确定',
            okType: 'danger',
            cancelText: '取消',
            onOk() {
                walkon(deviceInfo.deviceId,()=>{
                    deviceInfo.deviceStatus = 0
                });
            }
        });
    }
    /**
     * 打开编辑基本信息弹窗
     * @param oldInfo 旧设备信息.1.若无,则为新增;2.若有,则为编辑
     */
    const openDeviceBaseInfoModal = (oldInfo: any)=>{
        deviceBaseFormRef.value.resetFields()
        console.log(oldInfo )
        let deviceBaseForm2 = new DeviceBaseInfo()
        if(oldInfo){
            deviceBaseForm2.deviceId = oldInfo.deviceId
            deviceBaseForm2.deviceName = oldInfo.deviceName
            deviceBaseForm2.deviceSn = oldInfo.deviceSn
            deviceBaseForm2.status = oldInfo.deviceStatus
            deviceBaseForm2.deviceKind = oldInfo.deviceKind
            deviceBaseForm2.deviceColor = oldInfo.deviceColor
            deviceBaseForm2.deviceNumber = oldInfo.deviceNumber
            deviceBaseForm2.sectionId = oldInfo.idSection
            if (oldInfo.checkStatus == 1 || oldInfo.checkStatus == 2) {
                updateDevInfo.value = true
            }
        }
        Object.assign(deviceBaseForm.value, deviceBaseForm2)

        showDeviceBaseModal.value = true
    }


    return{
        deviceRef,
        deviceFormState,
        deviceList,
        resetForm,
        getDeviceList,

        getDeviceBaseInfo,
        nextDeviceList,
        inDeviceBaseInfo,

        deviceBaseFormRef,
        showDeviceBaseModal,
        deviceBaseForm,
        driverBaseRules,
        updateDevInfo,
        openWalkOff,openWalkOn,
        setDeviceBaseInfo,
        openDeviceBaseInfoModal
    }
}